package com.example.yk_fx;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.yk_fx.adapter.PlAdapter;
import com.example.yk_fx.adapter.VideoAdapter;
import com.example.yk_fx.base.BaseActivity;
import com.example.yk_fx.databinding.ActivityMainBinding;
import com.example.yk_fx.entity.PlEntity;
import com.example.yk_fx.entity.ResultEntity;
import com.example.yk_fx.entity.VideoEntity;
import com.example.yk_fx.video.VideoViewModel;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, VideoViewModel> {
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;
    int index;//当前视频的下标

    List<PlEntity.DataBean> pls = new ArrayList<>();
    PlAdapter plAdapter;

    @Override
    protected int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected Class<VideoViewModel> getViewModel() {
        return VideoViewModel.class;
    }

    @Override
    protected void initView() {
        requestPermissions(new String[]{
                "android.permission.VIBRATE",
                "android.permission.RECORD_AUDIO" ,
                "android.permission.CAMERA",
                "android.permission.ACCESS_NETWORK_STATE",
                "android.permission.WRITE_EXTERNAL_STORAGE" ,
                "android.permission.ACCESS_FINE_LOCATION",
                "android.permission.GET_TASKS" ,
                "android.permission.ACCESS_WIFI_STATE" ,
                "android.permission.CHANGE_WIFI_STATE" ,
                "android.permission.WAKE_LOCK",
                "android.permission.MODIFY_AUDIO_SETTINGS",
                "android.permission.READ_PHONE_STATE",
                "android.permission.RECEIVE_BOOT_COMPLETED",
                "android.permission.FOREGROUND_SERVICE",
                "android.permission.ACCESS_COARSE_LOCATION"
        },101);

        videoAdapter = new VideoAdapter(videos);
        vdb.rv.setAdapter(videoAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        vdb.rv.setLayoutManager(linearLayoutManager);
        vm.video(1,30);
        //分页更顺畅
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(vdb.rv);
        //自动播放
        vdb.rv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int position = linearLayoutManager.findFirstVisibleItemPosition();
                index = position;
                StandardGSYVideoPlayer gsy = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(position, R.id.gsy);

                gsy.startPlayLogic();
            }
        });
        videoAdapter.addChildClickViewIds(R.id.iv_dianzan,R.id.iv_pinglun,R.id.iv_close,R.id.iv_fasong,R.id.iv_quan,R.id.iv_live);
        videoAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId()){
                    case R.id.iv_dianzan:
                        dianzan(position);
                        break;
                    case R.id.iv_pinglun:
                        vdb.rv.setIntercept(false);
                        pinglun(position);//打开评论//解决滑动冲突
                        break;
                    case R.id.iv_close:
                        vdb.rv.setIntercept(true);
                        closepinglun(position);
                        break;
                    case R.id.iv_fasong:
                        sendpinglun(position);
                        break;
                    case R.id.iv_quan:
                        quan(position);
                        break;
                    case R.id.iv_live:
                        live(position);
                        break;
                }
            }
        });

    }

    private void live(int position) {
        Intent intent = new Intent(this, LiveActivity.class);
        intent.putExtra("video",videos.get(position));
        startActivity(intent);
    }

    private void quan(int position) {
        StandardGSYVideoPlayer gsy = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(position, R.id.gsy);
        gsy.startWindowFullscreen(this,false,true);
    }

    private void sendpinglun(int position) {
        EditText et = (EditText) videoAdapter.getViewByPosition(position,R.id.et_pinglun);
        String string = et.getText().toString();
        vm.sendPl(string,videos.get(position).getId());
        et.setText("");
    }

    private void closepinglun(int position) {
//找到评论框
        ConstraintLayout clPing = (ConstraintLayout) videoAdapter.getViewByPosition(position, R.id.cl_pinglun);
        clPing.setVisibility(View.GONE);
        //可添加动画
    }

    private void pinglun(int position) {
        //找到评论框
        ConstraintLayout clPing = (ConstraintLayout) videoAdapter.getViewByPosition(position, R.id.cl_pinglun);
        clPing.setVisibility(View.VISIBLE);
        //可添加动画
        //让editText获取焦点
        EditText editText = (EditText) videoAdapter.getViewByPosition(position, R.id.et_pinglun);
        editText.requestFocus();
        //加载评论的数据
        RecyclerView rvPl = (RecyclerView) videoAdapter.getViewByPosition(position, R.id.rv_pinglun);
        plAdapter = new PlAdapter(R.layout.item_pinglun,pls);
        rvPl.setAdapter(plAdapter);
        rvPl.setLayoutManager(new LinearLayoutManager(this));
        vm.getPl(videos.get(position).getId());
    }

    private void dianzan(int position) {
        if(SPUtils.getInstance().getString("token").isEmpty()){//未登录，不能点赞，，跳转到登录界面
            startActivity(new Intent(this,LoginActivity.class));
        }
        //判断当时登录人对视频进行过点赞吗
        if(videos.get(position).getDianzan()==1){//点赞过
            //取消点赞
            vm.nolove(videos.get(position).getId());
        }else {//未点赞过
            //点赞
            vm.love(videos.get(position).getId());
        }
    }

    @Override
    protected void initData() {
        vm.success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                videos.addAll(entity.getData());
                videoAdapter.notifyDataSetChanged();
            }
        });
        vm.loveSucc.observe(this, new Observer<ResultEntity>() {
            @Override
            public void onChanged(ResultEntity entity) {
                ToastUtils.showLong(entity.getData());
                //将对应的点赞值更新---添加动画效果
                videos.get(index).setDianzan(1);
                ImageView ivLove = (ImageView) videoAdapter.getViewByPosition(index, R.id.iv_dianzan);
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivLove, "scaleX", 1, 2, 1);
                ObjectAnimator animator = ObjectAnimator.ofFloat(ivLove, "scaleY", 1, 2, 1);
                AnimatorSet set = new AnimatorSet();
                set.setDuration(2000);
                set.play(scaleX).with(animator);
                set.start();
            }
        });
        vm.noloveSucc.observe(this, new Observer<ResultEntity>() {
            @Override
            public void onChanged(ResultEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                videos.get(index).setDianzan(0);
            }
        });
        vm.getSucc.observe(this, new Observer<PlEntity>() {
            @Override
            public void onChanged(PlEntity entity) {
                pls.addAll(entity.getData());
                plAdapter.notifyDataSetChanged();
            }
        });
        vm.sendSucc.observe(this, new Observer<PlEntity>() {
            @Override
            public void onChanged(PlEntity entity) {
                pls.add(0,entity.getData().get(0));
                plAdapter.notifyDataSetChanged();
            }
        });
    }
}